-- card: 38514 from stack: in.5 -- bmap block id: 43542 -- flags: 0000 -- background id: 3858 -- name: NumToBinary ----- HyperTalk script ----- on hideObjects hide cd fld "label" hide cd fld "label 2" hide cd fld "decimal" hide cd fld "binary" hide cd btn "convert" end hideObjects on showObjects show cd fld "label" show cd fld "label 2" show cd fld "decimal" show cd fld "binary" show cd btn "convert" end showObjects -- part 1 (button) -- low flags: 00 -- high flags: A002 -- rect: left=82 top=191 right=225 bottom=175 -- title width / last selected line: 0 -- icon id / first selected line: 0 / 0 -- text alignment: 1 -- font id: 0 -- text size: 12 -- style flags: 8192 -- line height: 16 -- part name: Convert ----- HyperTalk script ----- on mouseUp put NumToBinary(cd fld "decimal") into cd fld "binary" end mouseUp -- part 2 (field) -- low flags: 00 -- high flags: 0002 -- rect: left=103 top=159 right=179 bottom=240 -- title width / last selected line: 0 -- icon id / first selected line: 0 / 0 -- text alignment: 0 -- font id: 3 -- text size: 12 -- style flags: 256 -- line height: 16 -- part name: Decimal ----- HyperTalk script ----- on ReturnInField send mouseUp to cd btn "convert" end ReturnInField on EnterInField send mouseUp to cd btn "convert" end EnterInField -- part 3 (field) -- low flags: 00 -- high flags: 0002 -- rect: left=103 top=234 right=254 bottom=240 -- title width / last selected line: 0 -- icon id / first selected line: 0 / 0 -- text alignment: 0 -- font id: 3 -- text size: 12 -- style flags: 256 -- line height: 16 -- part name: Binary -- part 4 (field) -- low flags: 00 -- high flags: 0000 -- rect: left=45 top=161 right=178 bottom=96 -- title width / last selected line: 0 -- icon id / first selected line: 0 / 0 -- text alignment: 65535 -- font id: 3 -- text size: 10 -- style flags: 0 -- line height: 13 -- part name: label -- part 5 (field) -- low flags: 00 -- high flags: 0000 -- rect: left=45 top=237 right=254 bottom=96 -- title width / last selected line: 0 -- icon id / first selected line: 0 / 0 -- text alignment: 65535 -- font id: 3 -- text size: 10 -- style flags: 0 -- line height: 13 -- part name: label 2 -- part contents for background part 38 ----- text ----- 32/50 -- part contents for card part 4 ----- text ----- Decimal -- part contents for card part 5 ----- text ----- Binary -- part contents for card part 2 ----- text ----- 253 -- part contents for card part 3 ----- text ----- 11111101 -- part contents for background part 20 ----- text ----- This XFCN returns the binary representation (base 2, all 1's and 0's) of a decimal number. Calling syntax : NumToBinary(decimalNumber) DECIMALNUMBER: the number to convert. -- part contents for background part 42 ----- text ----- {NumToBinary(theNumber)} { Change a number to a string representing it's binary value. } {} { brought to you by: Anup Murarka Eric Carlson } { ALINK: SKEPTIC ALINK: cyNic } { CIS: 76004,3356 } {} { We are part of the Support Tools Development Group, } { Apple Computer, Inc. } {} { please DO NOT contack Mac DTS for support of this code! } {} { please DO contact the authors for support of this code! } {} { Send comments, bug reports, requests to any of the above } { E-mail addresses or to:} {} { (one of us) } { Apple Computer, Inc. } { 900 E. Hamilton, Ave. } { Campbell, CA 95008 } { M/S 72-L } {} { Copyright: © 1989, 1990 by Apple Computer, Inc., all rights reserved. } {} { written by Eric Carlson } { AppleLink: cyNic } { modification history } { Date Initials Comments } { ---- ------ --------------------------------------------------} { 11/20/89 ec first written } { 6/6/90 ec commented, cleaned code } {} unit NumToBin; interface uses HyperXCMD; procedure main (paramPtr: XCmdPtr); implementation function askedForHelp (paramPtr: XCmdPtr; syntaxMsg: Str255; copyRightMsg: Str255): boolean; {} { check to see if the user sent a '?' or a '!' as } { the only parameter. if so we will respond with } { the calling syntax or the copyright/version info } { for this external } {} var firstStr: str255; begin askedForHelp := false; if paramPtr^.paramCount = 1 then begin ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr); { what is the first param? } if firstStr = '?' then begin paramPtr^.returnValue := PasToZero(paramPtr, syntaxMsg); askedForHelp := true end { asked for help } else if firstStr = '!' then begin paramPtr^.returnValue := PasToZero(paramPtr, copyRightMsg); askedForHelp := true end; { asked for copyright info } end; { one parameter passed } end; { function } function NumberToString (paramPtr: XCmdPtr; num: LONGINT): Str255; { use the toolbox call rather than HC's } var tempStr: str255; begin NumToString(num, tempStr); NumberToString := tempStr; end; procedure NumToBin (paramPtr: XCMDPtr); var copyRtStr, syntaxStr: str255; binaryNum: str255; theNumber: LONGINT; bitCount, bits: integer; binArray: string[2]; begin syntaxStr := 'NumToBinary(theNumber)'; copyRtStr := '© 1989,1990 Apple Computer, Inc. v.1.0, by Eric Carlson.'; if askedForHelp(paramPtr, syntaxStr, copyRtStr) then { asking for help? } exit(NumToBin); ZeroToPas(paramPtr, paramPtr^.params[1]^, binaryNum); { the value to convert } theNumber := StrToNum(paramPtr, binaryNum); binArray := '01'; { we'll return some combination of these } bitCount := round(ln(theNumber) / ln(2)); { the number of bits in our number } if (bitCount mod 8 <> 0) or (bitCount = 0) then { does our number have an odd number of bits? } bitCount := (bitCount div 8 + 1) * 8; { make it an even multiple of 8 } binaryNum := binArray[BAND(theNumber, $01) + 1]; { grab the first bit } for bits := 1 to bitCount - 1 do { and step through the rest } begin theNumber := BSR(theNumber, 1); { shift right, drop the bit we just examined } binaryNum := concat(binArray[BAND(theNumber, $01) + 1], binaryNum); end; paramPtr^.returnValue := PasToZero(paramPtr, binaryNum); end; procedure main (paramPtr: XCmdPtr); begin NumToBin(paramPtr); end; end.